package com.cyk.Servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


@WebServlet("/upLoadServlet")
public class UpLoadServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取上传文件的组件DiskFileItemFactory
		DiskFileItemFactory factory = new DiskFileItemFactory();
		/*
		 * 分别设置临时存储文件的目录以及内存大小
		 */
		factory.setSizeThreshold(1024*100);
		String tempPath = this.getServletContext().getRealPath("/WEB-INF/temp");
		System.out.println(tempPath);
		File tempfile = new File(tempPath);
		if(tempfile.exists()) {
			tempfile.mkdir();
		}
		factory.setRepository(tempfile);
		//获取servlet上传文件的文件解析器，并设置相关参数
		ServletFileUpload upload = new ServletFileUpload(factory);
		//判断下当前请求是否包含文件内容，不包含的话直接结束
		if (!ServletFileUpload.isMultipartContent(request)) {
			return;
		}
		upload.setHeaderEncoding("UTF-8");
		upload.setFileSizeMax(1024*1024);  //设置当个文件1M
		upload.setSizeMax(1024*1024*10);   //全部文件10M
		//利用解析器解析发送过来的请求
		try {
			//提交过来的表单中的每一个输入项就是一个fileItem
			List<FileItem> list = upload.parseRequest(request);
			for (FileItem fileItem : list) {
				//如果此输入项是一个普通表单属性
				if (fileItem.isFormField()) {
					String fieldName = fileItem.getFieldName();
					String name = fileItem.getString("utf-8");
					//System.out.println(fieldName+"\n"+name);
					continue;
				}else {
					//获取提交文件的名字,注意在某些浏览器下获取的是文件的绝对路劲
					String name = fileItem.getName();
					if (null == name || name.trim().equals("")) {
						continue;
					}
					//获取上传文件的输入流
					InputStream inputStream = fileItem.getInputStream();
					File file = new File("C:\\file");
					if (!file.exists()) {
						file.mkdir();
					}
					name = "C:\\file\\"+name;
					FileOutputStream outputStream = new FileOutputStream(name);
					System.out.println("------");
					//新建一个1M的缓冲区
					byte[] buffer = new byte[1024];
					int length = 0;
					//将输入流中的东西读入缓冲区，当全部读进去返回的是-1,>0表示输入流中还有内容
					while((length = inputStream.read(buffer))>0) {
						//循环这将文件通过输出流写入本地文件
						outputStream.write(buffer, 0, length);
					}
					//关闭输入输出资源
					inputStream.close();
					outputStream.close();
				}
				
				
			}
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}

}
